<!DOCTYPE html>
<html lang=zh>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="description" content="虚拟DOM的优势现如今的三大框架都是使用虚拟DOM来对真实DOM进行抽象，这样做的好处有：  方便对其操作 方便进行跨平台的操作，将虚拟节点渲染成你想要的节点  虚拟DOM的渲染过程 Vue源码的核心Vue源码有三大核心：  Compiler模块：用来编译模板系统 Runtime模块：也就是渲染器模块 Reactivity模块：响应式系统   Mini-Vue的实现主要从三部分实现：  渲染系统模">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue源码学习">
<meta property="og:url" content="https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/index.html">
<meta property="og:site_name" content="柯里的语法糖">
<meta property="og:description" content="虚拟DOM的优势现如今的三大框架都是使用虚拟DOM来对真实DOM进行抽象，这样做的好处有：  方便对其操作 方便进行跨平台的操作，将虚拟节点渲染成你想要的节点  虚拟DOM的渲染过程 Vue源码的核心Vue源码有三大核心：  Compiler模块：用来编译模板系统 Runtime模块：也就是渲染器模块 Reactivity模块：响应式系统   Mini-Vue的实现主要从三部分实现：  渲染系统模">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20220113202216941.png">
<meta property="og:image" content="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20220113202733444.png">
<meta property="og:image" content="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20220116104342910.png">
<meta property="article:published_time" content="2022-02-05T06:19:40.066Z">
<meta property="article:modified_time" content="2022-02-05T06:19:40.066Z">
<meta property="article:author" content="柯里的语法糖">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20220113202216941.png">
    
    
      
        
          <link rel="shortcut icon" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico">
        
      
      
        
          <link rel="icon" type="image/png" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico" sizes="192x192">
        
      
      
        
          <link rel="apple-touch-icon" sizes="180x180" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico">
        
      
    
    <!-- title -->
    <title>Vue源码学习</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
    <!-- rss -->
    
    
	<!-- mathjax -->
	
<meta name="generator" content="Hexo 6.0.0"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#" aria-label="Menu"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#" aria-label="Menu"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" aria-label="Top" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" aria-label="上一篇" href="/2022/02/05/frontend/vuex_map/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" aria-label="下一篇" href="/2022/02/05/frontend/vue_nextTick/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" aria-label="返回顶部" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" aria-label="分享文章" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&text=Vue源码学习"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&title=Vue源码学习"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&is_video=false&description=Vue源码学习"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=Vue源码学习&body=Check out this article: https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&title=Vue源码学习"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&title=Vue源码学习"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&title=Vue源码学习"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&title=Vue源码学习"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&name=Vue源码学习&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&t=Vue源码学习"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%99%9A%E6%8B%9FDOM%E7%9A%84%E4%BC%98%E5%8A%BF"><span class="toc-number">1.</span> <span class="toc-text">虚拟DOM的优势</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%99%9A%E6%8B%9FDOM%E7%9A%84%E6%B8%B2%E6%9F%93%E8%BF%87%E7%A8%8B"><span class="toc-number">2.</span> <span class="toc-text">虚拟DOM的渲染过程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue%E6%BA%90%E7%A0%81%E7%9A%84%E6%A0%B8%E5%BF%83"><span class="toc-number">3.</span> <span class="toc-text">Vue源码的核心</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Mini-Vue%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-number">4.</span> <span class="toc-text">Mini-Vue的实现</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B8%B2%E6%9F%93%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-number">4.1.</span> <span class="toc-text">渲染系统的实现</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#h%E5%87%BD%E6%95%B0"><span class="toc-number">4.1.1.</span> <span class="toc-text">h函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#mount%E5%87%BD%E6%95%B0"><span class="toc-number">4.1.2.</span> <span class="toc-text">mount函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#patch%E5%87%BD%E6%95%B0"><span class="toc-number">4.1.3.</span> <span class="toc-text">patch函数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#reactivity%E5%87%BD%E6%95%B0"><span class="toc-number">4.2.</span> <span class="toc-text">reactivity函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BAapp%E5%AF%B9%E8%B1%A1%E7%9A%84%E8%BF%87%E7%A8%8B"><span class="toc-number">4.3.</span> <span class="toc-text">创建app对象的过程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8C%82%E8%BD%BD%E6%A0%B9%E7%BB%84%E4%BB%B6%E7%9A%84%E8%BF%87%E7%A8%8B"><span class="toc-number">4.4.</span> <span class="toc-text">挂载根组件的过程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#setupComponent%E5%87%BD%E6%95%B0%E7%9A%84%E5%86%85%E9%83%A8%E7%BB%86%E8%8A%82"><span class="toc-number">4.5.</span> <span class="toc-text">setupComponent函数的内部细节</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#compile%E7%9A%84%E8%BF%87%E7%A8%8B"><span class="toc-number">4.6.</span> <span class="toc-text">compile的过程</span></a></li></ol></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        Vue源码学习
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">柯里的语法糖</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2022-02-05T06:19:40.066Z" itemprop="datePublished">2022-02-05</time>
        
      
    </div>


      
    <div class="article-category">
        <i class="fas fa-archive"></i>
        <a class="category-link" href="/categories/%E5%AE%9E%E6%88%98%E5%BC%80%E5%8F%91/">实战开发</a>
    </div>


      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link-link" href="/tags/Vue/" rel="tag">Vue</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h2 id="虚拟DOM的优势"><a href="#虚拟DOM的优势" class="headerlink" title="虚拟DOM的优势"></a>虚拟DOM的优势</h2><p>现如今的三大框架都是使用虚拟DOM来对真实DOM进行抽象，这样做的好处有：</p>
<ol>
<li>方便对其操作</li>
<li>方便进行跨平台的操作，将虚拟节点渲染成你想要的节点</li>
</ol>
<h2 id="虚拟DOM的渲染过程"><a href="#虚拟DOM的渲染过程" class="headerlink" title="虚拟DOM的渲染过程"></a>虚拟DOM的渲染过程</h2><p><img src="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20220113202216941.png" alt="image-20220113202216941"></p>
<h2 id="Vue源码的核心"><a href="#Vue源码的核心" class="headerlink" title="Vue源码的核心"></a>Vue源码的核心</h2><p>Vue源码有三大核心：</p>
<ol>
<li>Compiler模块：用来编译模板系统</li>
<li>Runtime模块：也就是渲染器模块</li>
<li>Reactivity模块：响应式系统</li>
</ol>
<p><img src="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20220113202733444.png" alt="image-20220113202733444"></p>
<h2 id="Mini-Vue的实现"><a href="#Mini-Vue的实现" class="headerlink" title="Mini-Vue的实现"></a>Mini-Vue的实现</h2><p>主要从三部分实现：</p>
<ol>
<li>渲染系统模块</li>
<li>响应式系统模块</li>
<li>应用程序入口模块</li>
</ol>
<h3 id="渲染系统的实现"><a href="#渲染系统的实现" class="headerlink" title="渲染系统的实现"></a>渲染系统的实现</h3><p>主要包括三个功能：</p>
<ol>
<li>h函数：返回一个vnode对象</li>
<li>mount函数：用于将vnode对象挂载到DOM上</li>
<li>patch函数：用于将两个vnode对比，决定如何处理新的vnode</li>
</ol>
<h4 id="h函数"><a href="#h函数" class="headerlink" title="h函数"></a>h函数</h4><p>直接返回一个vnode对象即可:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> h = <span class="function">(<span class="params">tag, props, children</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// vnode -&gt; Object</span></span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    tag,</span><br><span class="line">    props,</span><br><span class="line">    children,</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h4 id="mount函数"><a href="#mount函数" class="headerlink" title="mount函数"></a>mount函数</h4><p>mount函数的步骤主要有四步：</p>
<ol>
<li>创建真实DOM元素</li>
<li>处理vnode的props中的属性</li>
<li>处理vnode的children</li>
<li>将vnode挂载到真实DOM上</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> mount = <span class="function">(<span class="params">vnode, container</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 1. create element</span></span><br><span class="line">  <span class="keyword">const</span> el = (vnode.el = <span class="built_in">document</span>.createElement(vnode.tag));</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 2. handle Props</span></span><br><span class="line">  <span class="keyword">if</span> (vnode.props) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">const</span> key <span class="keyword">in</span> vnode.props) &#123;</span><br><span class="line">      <span class="keyword">const</span> value = vnode.props[key];</span><br><span class="line">      <span class="keyword">if</span> (<span class="keyword">typeof</span> value === <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line">        el.setAttribute(key, value);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        el.addEventListener(key.slice(<span class="number">2</span>).toLowerCase(), value);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 3. handle children</span></span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">typeof</span> vnode.children === <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line">    el.textContent = vnode.children;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    vnode.children.forEach(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">      mount(item, el);</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 4. mount on container</span></span><br><span class="line">  container.appendChild(el);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h4 id="patch函数"><a href="#patch函数" class="headerlink" title="patch函数"></a>patch函数</h4><p>patch函数的实现主要有四步：</p>
<ol>
<li>比较标签是否相同</li>
<li>如果标签不同，则处理props，处理新的props，删除旧的props</li>
<li>继续处理children属性，对不同的类型进行比较、判断</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> patch = <span class="function">(<span class="params">n1, n2</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 1. diff tag</span></span><br><span class="line">  <span class="keyword">if</span> (n1.tag !== n2.tag) &#123;</span><br><span class="line">    <span class="keyword">const</span> n1ElParent = n1.el.parent;</span><br><span class="line">    n1ElParent.removeChild(n1.el);</span><br><span class="line">    mount(n2, n1ElParent);</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> el = (n2.el = n1.el);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> oldProps = n1.props || &#123;&#125;;</span><br><span class="line">    <span class="keyword">const</span> newProps = n2.props || &#123;&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2. handle newProp</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">const</span> key <span class="keyword">in</span> newProps) &#123;</span><br><span class="line">      <span class="keyword">const</span> newValue = newProps[key];</span><br><span class="line">      <span class="keyword">const</span> oldValue = oldProps[key];</span><br><span class="line">      <span class="comment">// dif  f propsValue</span></span><br><span class="line">      <span class="keyword">if</span> (newValue !== oldValue) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">typeof</span> newValue === <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line">          el.setAttribute(key, newValue);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          el.addEventListener(key.slice(<span class="number">2</span>).toLowerCase(), newValue);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3. delete oldProp</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">const</span> key <span class="keyword">in</span> oldProps) &#123;</span><br><span class="line">      <span class="keyword">const</span> oldValue = oldProps[key];</span><br><span class="line">      <span class="comment">// 3.1 remove all event</span></span><br><span class="line">      <span class="keyword">if</span> (key.startsWith(<span class="string">&quot;on&quot;</span>)) &#123;</span><br><span class="line">        el.removeEventListener(key.slice(<span class="number">2</span>).toLowerCase(), oldValue);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 3.2 key not in newProps</span></span><br><span class="line">      <span class="keyword">if</span> (!(key <span class="keyword">in</span> newProps)) &#123;</span><br><span class="line">        el.removeAttribute(key);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4.handle children</span></span><br><span class="line">    <span class="keyword">const</span> oldChildren = n1.children || [];</span><br><span class="line">    <span class="keyword">const</span> newChildren = n2.children || [];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> newChildren === <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line">      <span class="comment">// 4.1 newChildren -&gt; String oldChildren -&gt; String</span></span><br><span class="line">      <span class="keyword">if</span> (<span class="keyword">typeof</span> oldChildren === <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line">        el.textContent = newChildren;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 4.2 newChildren -&gt; String oldChildren -&gt; Array</span></span><br><span class="line">        el.innerHTML = newChildren;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">// 4.3 newChildren -&gt; Array oldChildren -&gt; String</span></span><br><span class="line">      <span class="keyword">if</span> (<span class="keyword">typeof</span> oldChildren === <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line">        newChildren.forEach(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">          mount(item, el);</span><br><span class="line">        &#125;);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 4.4 newChildren -&gt; Array oldChildren -&gt; Array</span></span><br><span class="line">        <span class="keyword">const</span> commonLength = <span class="built_in">Math</span>.min(oldChildren.length, newChildren.length);</span><br><span class="line">        <span class="comment">// 4.4.1 patch all</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; commonLength; i++) &#123;</span><br><span class="line">          patch(oldChildren[i], newChildren[i]);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 4.4.2 newChildren.length &gt; oldChildren.length</span></span><br><span class="line">        <span class="keyword">if</span> (newChildren.length &gt; oldChildren.length) &#123;</span><br><span class="line">          newChildren.slice(oldChildren.length).forEach(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">            mount(item, el);</span><br><span class="line">          &#125;);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 4.4.3 newChildren.length &lt; oldChildren.length</span></span><br><span class="line">        <span class="keyword">if</span> (newChildren.length &lt; oldChildren.length) &#123;</span><br><span class="line">          oldChildren.slice(newChildren.length).forEach(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">            el.removeChild(item.el);</span><br><span class="line">          &#125;);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="reactivity函数"><a href="#reactivity函数" class="headerlink" title="reactivity函数"></a>reactivity函数</h3><h3 id="创建app对象的过程"><a href="#创建app对象的过程" class="headerlink" title="创建app对象的过程"></a>创建app对象的过程</h3><ol>
<li><p>创建app对象</p>
<ol>
<li><p>调用ensureRenderer函数，创建渲染器</p>
</li>
<li><p>调用baseCreateRender函数，里面包含render函数的实现，此函数的返回值为：</p>
<p><img src="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20220116104342910.png" alt="image-20220116104342910"></p>
</li>
<li><p>creatAppAPI调用的返回值是一个函数，该函数的返回值才是返回的真正的app对象</p>
</li>
<li></li>
</ol>
</li>
<li><p>重写app对象的mount方法</p>
</li>
<li><p>返回app对象</p>
</li>
</ol>
<h3 id="挂载根组件的过程"><a href="#挂载根组件的过程" class="headerlink" title="挂载根组件的过程"></a>挂载根组件的过程</h3><ol>
<li>调用app对象中重写的mount方法</li>
<li>调用createAppAPI返回的函数，此函数返回的app对象的mount方法</li>
<li>判断是否挂载</li>
<li>创建根组件的vnode</li>
<li>调用render函数，渲染vnode</li>
<li>进入render函数，调用patch函数，此函数实现的是创建或者更新组件</li>
<li>判断类型</li>
<li>调用processComponent函数，对组件进行处理</li>
<li>调用mountComponent挂载组件</li>
<li>调用createComponentInstance函数创建组件的实例</li>
<li>调用setupComponent函数，此函数是对组件的props/data/methods/computed等进行初始化处理，并且内部实现对vue2的options api的兼容</li>
<li>调用setupRenderEffect，此函数是设置和渲染有副作用的函数，用来监听组件数据的变化</li>
<li>判断组件是否挂载</li>
<li>获取组件的subTree</li>
<li>调用patch函数</li>
<li>判断类型</li>
<li>是element类型</li>
<li>调用processElement，对元素类型进行处理</li>
<li>调用mountElement，此函数用来挂载元素</li>
<li>根据vnode创建元素</li>
<li>判断情况，如果子节点是一个数组，调用mountChildren挂载子元素</li>
<li>挂载完成</li>
</ol>
<blockquote>
<p>组件的vnode和instance的区别：</p>
<p>​    组件的vnode：虚拟dom中的虚拟节点</p>
<p>​    组件的instance：保存组件的状态：如data、methods、computed等</p>
</blockquote>
<h3 id="setupComponent函数的内部细节"><a href="#setupComponent函数的内部细节" class="headerlink" title="setupComponent函数的内部细节"></a>setupComponent函数的内部细节</h3><ol>
<li>判断是否是一个有状态的组件</li>
<li>初始化props和slots</li>
<li>调用setupStatefulComponent函数，对有状态的组件进行设置</li>
<li>判断是否有setup函数</li>
<li>如果setup有参数, 那么会创建一个setupContext上下文, 这个上下文可以在函数中被使用</li>
<li>调用callWithErrorHandling函数得到setup函数的结果</li>
<li>调用handleSetupResult，处理setup函数的结果</li>
<li>调用finishComponentSetup，此函数内部调用compile函数对template进行处理运行时模板，完成组件实例化设置</li>
</ol>
<h3 id="compile的过程"><a href="#compile的过程" class="headerlink" title="compile的过程"></a>compile的过程</h3><ol>
<li>其实就是调用了一个叫baseCompile的函数</li>
<li>对template进行解析生成ast树</li>
<li>进行ast转换</li>
<li>生成代码</li>
</ol>

  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/search/">搜索</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%99%9A%E6%8B%9FDOM%E7%9A%84%E4%BC%98%E5%8A%BF"><span class="toc-number">1.</span> <span class="toc-text">虚拟DOM的优势</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%99%9A%E6%8B%9FDOM%E7%9A%84%E6%B8%B2%E6%9F%93%E8%BF%87%E7%A8%8B"><span class="toc-number">2.</span> <span class="toc-text">虚拟DOM的渲染过程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue%E6%BA%90%E7%A0%81%E7%9A%84%E6%A0%B8%E5%BF%83"><span class="toc-number">3.</span> <span class="toc-text">Vue源码的核心</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Mini-Vue%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-number">4.</span> <span class="toc-text">Mini-Vue的实现</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B8%B2%E6%9F%93%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-number">4.1.</span> <span class="toc-text">渲染系统的实现</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#h%E5%87%BD%E6%95%B0"><span class="toc-number">4.1.1.</span> <span class="toc-text">h函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#mount%E5%87%BD%E6%95%B0"><span class="toc-number">4.1.2.</span> <span class="toc-text">mount函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#patch%E5%87%BD%E6%95%B0"><span class="toc-number">4.1.3.</span> <span class="toc-text">patch函数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#reactivity%E5%87%BD%E6%95%B0"><span class="toc-number">4.2.</span> <span class="toc-text">reactivity函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BAapp%E5%AF%B9%E8%B1%A1%E7%9A%84%E8%BF%87%E7%A8%8B"><span class="toc-number">4.3.</span> <span class="toc-text">创建app对象的过程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8C%82%E8%BD%BD%E6%A0%B9%E7%BB%84%E4%BB%B6%E7%9A%84%E8%BF%87%E7%A8%8B"><span class="toc-number">4.4.</span> <span class="toc-text">挂载根组件的过程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#setupComponent%E5%87%BD%E6%95%B0%E7%9A%84%E5%86%85%E9%83%A8%E7%BB%86%E8%8A%82"><span class="toc-number">4.5.</span> <span class="toc-text">setupComponent函数的内部细节</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#compile%E7%9A%84%E8%BF%87%E7%A8%8B"><span class="toc-number">4.6.</span> <span class="toc-text">compile的过程</span></a></li></ol></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&text=Vue源码学习"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&title=Vue源码学习"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&is_video=false&description=Vue源码学习"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=Vue源码学习&body=Check out this article: https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&title=Vue源码学习"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&title=Vue源码学习"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&title=Vue源码学习"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&title=Vue源码学习"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&name=Vue源码学习&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://danyuan30.gitee.io/2022/02/05/frontend/vue_source/&t=Vue源码学习"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
    
    
    2021-2022
    柯里的语法糖
  </div>
  <div class="footer-right">
    <nav>
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->



  <link rel="preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" crossorigin="anonymous" onload="this.onload=null;this.rel='stylesheet'"/>


    <!-- jquery -->
 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script> 




<!-- clipboard -->

  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.7/clipboard.min.js" crossorigin="anonymous"></script> 
  
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"复制到粘贴板!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "复制成功!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Cloudflare Analytics -->

<!-- Umami Analytics -->

<!-- Disqus Comments -->

<!-- utterances Comments -->

</body>
</html>
